﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FUI;
using System.IO;
using System.Text.RegularExpressions;
using CUILoader;
using FUINetCF;

namespace NumericUpDownFUI
{
    class CNumericUpDownFUI :FUI.FUIBase
    {
        public CNumericUpDownFUI()
        {
            StrName = "NetCFNumericUpDown";
            loadXMLProperties();
        }

        public override string generateDeclaration()
        {
            FileStream file;
            file = new FileStream(StrPath + "\\NetCFNumericUpDownDeclaration.txt", FileMode.OpenOrCreate, FileAccess.Read);
            StreamReader sr = new StreamReader(file);
            string s = sr.ReadToEnd();
            sr.Close();
            file.Close();
            string pattern = "###NumericUpDownName###";
            string replacement = generateDataType(this.getAttributeFromName("Name"));
            Regex rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);
            return s;
        }

        public override string generateInitialization()
        {
            FileStream file;
            file = new FileStream(StrPath + "\\NetCFNumericUpDownInitialization.txt", FileMode.OpenOrCreate, FileAccess.Read);
            StreamReader sr = new StreamReader(file);
            string s = sr.ReadToEnd();
            sr.Close();
            file.Close();

            //Su dung regex de thay cac pattern trong Initialization.txt

            string pattern = "###NumericUpDownName###";
            string replacement = generateDataType(this.getAttributeFromName("Name"));
            Regex rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            string strTemp = generateDataType(this.getAttributeFromName("Value"));
            decimal decimalTemp = 10.0M;
            int[] intArray = Decimal.GetBits(decimalTemp);
            try
            {
                decimalTemp = Decimal.Parse(strTemp);
                intArray = Decimal.GetBits(decimalTemp);
            }
            catch { };
            replacement = "";
            foreach (int i in intArray)
            {
                replacement += "\n\t";
                replacement += i.ToString();
                replacement += ",";
            }
            replacement = replacement.TrimEnd(new char[] { ',' });
            pattern = "###Value###";            
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            strTemp = generateDataType(this.getAttributeFromName("Maximum"));
            decimalTemp = 20M;
            intArray = Decimal.GetBits(decimalTemp);
            try
            {
                decimalTemp = Decimal.Parse(strTemp);
                intArray = Decimal.GetBits(decimalTemp);
            }
            catch { };
            replacement = "";
            foreach (int i in intArray)
            {
                replacement += "\n\t";
                replacement += i.ToString();
                replacement += ",";
            }
            replacement = replacement.TrimEnd(new char[] { ',' });
            pattern = "###Maximum###";
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            strTemp = generateDataType(this.getAttributeFromName("Minimum"));
            decimalTemp = 0M;
            intArray = Decimal.GetBits(decimalTemp);
            try
            {
                decimalTemp = Decimal.Parse(strTemp);
                intArray = Decimal.GetBits(decimalTemp);
            }
            catch { };
            replacement = "";
            foreach (int i in intArray)
            {
                replacement += "\n\t";
                replacement += i.ToString();
                replacement += ",";
            }
            replacement = replacement.TrimEnd(new char[] { ',' });
            pattern = "###Minimum###";
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###IsEnable###";
            replacement = generateDataType(this.getAttributeFromName("IsEnable"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###FontSize###";
            replacement = generateDataType(this.getAttributeFromName("FontSize"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###FontStyle###";
            replacement = generateDataType(this.getAttributeFromName("FontStyle"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###ForeColor###";
            replacement = generateDataType(this.getAttributeFromName("ForeColor"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###XLocation###";
            replacement = generateDataType(this.getAttributeFromName("XLocation"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###YLocation###";
            replacement = generateDataType(this.getAttributeFromName("YLocation"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###Name###";
            replacement = generateDataType(this.getAttributeFromName("Name"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###WSize###";
            replacement = generateDataType(this.getAttributeFromName("WSize"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###HSize###";
            replacement = generateDataType(this.getAttributeFromName("HSize"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);

            pattern = "###IsVisible###;";
            replacement = generateDataType(this.getAttributeFromName("IsVisible"));
            rgx = new Regex(pattern);
            s = rgx.Replace(s, replacement);
            return s;
        }

        public override string generateDataType(CAttribute m_Att)
        {
            CNetCFConverter m_NetCFConverter = new CNetCFConverter();
            return m_NetCFConverter.NetCFGenerateDataType(m_Att);
        }
    }
}
